{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "69f38f18-cf7b-4ba8-b707-52c200c34fcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torchvision as tv\n",
    "import torch.nn as nn\n",
    "import torchvision.transforms as transforms\n",
    "from torch.utils.data import DataLoader, Dataset\n",
    "import numpy as np\n",
    "import os\n",
    "import os.path as osp\n",
    "import matplotlib.pyplot as plt\n",
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45607ec8-f095-4799-8ccc-6a30a0e21987",
   "metadata": {},
   "source": [
    "# Load Efficientnet from network "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53928672-524b-48a2-8ae5-e25c86f37383",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model = tv.models.efficientnet_b7(pretrained=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9c7e2c05-38c6-446b-af2e-967959aceb8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.classifier[1] = nn.Sequential(\n",
    "    nn.Linear(model.classifier[1].in_features, 1, bias=True),\n",
    "    nn.Sigmoid()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22d37a82-0d6f-43d6-8edb-d18c13048888",
   "metadata": {},
   "outputs": [],
   "source": [
    "归一化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc8b891d-ddc8-489f-a77f-13a0886422d6",
   "metadata": {},
   "source": [
    "# inherit the Dataset class defined in pytorch and overload the magic methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9f1b7f49-f85b-445a-9342-f4a297a9250a",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Loader(Dataset):\n",
    "    # preprocess when create instance\n",
    "    # data is tensor which  is of the shape of (batch, channel, height, width)\n",
    "    # we assume that image are read by func \"plt.imread\"\n",
    "    # we must transform it to tensor and normalized it to the same size\n",
    "    def __init__(data, target):\n",
    "        trans = transforms.Compose([\n",
    "            transforms.ToTensor(),\n",
    "            transforms.Resize([224, 224])\n",
    "        ])\n",
    "        self.data = trans(data)\n",
    "        self.target = torch.tensor(target).type(torch.float32)\n",
    "        \n",
    "    def __getitem__(self, index):\n",
    "        return self.data[index], self.target[index]\n",
    "\n",
    "    def __len__(self):\n",
    "        return self.target.size(0)\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36450bc8-65d6-4860-b257-4527174e38b3",
   "metadata": {},
   "source": [
    "# define a function to read pictures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2a6bce2-455e-45b0-ab13-6cdced9f7903",
   "metadata": {},
   "outputs": [],
   "source": [
    "trans = transforms.Compose([\n",
    "            transforms.ToTensor(),\n",
    "            transforms.Resize([224, 224])\n",
    "        ])\n",
    "def read_pics(img_raw_path):\n",
    "    target = []\n",
    "    imgs = torch.tensor()\n",
    "    for f in os.listdir(img_raw_path):\n",
    "        pn, fn = osp.split(f)\n",
    "        target.append(1 if \"normal\" in fn else 0)\n",
    "        img = plt.imread(f)\n",
    "        img = trans(img).unsqueeze(0)\n",
    "        imgs = torch.cat((img, imgs if len(imgs) != 0 else torch.tensor([])), dim = 0)\n",
    "        给it给it给it\n",
    "        \n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
